<?php


namespace app\api\controller;


use app\data\controller\api\Auth;
use app\data\model\DataQiyeRzinfo;
use app\data\model\DataUser;
use app\data\model\DataUserExtension;
use app\data\model\DataUserReview;
use app\data\model\RecruitmentReview;
use app\data\model\RecruitmentTalent;

class Recruitment extends Auth
{
    /**
     * 人才招聘列表
     * @throws \think\db\exception\DbException
     */
    public function list()
    {
        $limit = input('limit');
        $page  = input('page');
        $list  = RecruitmentTalent::query()
            ->order('effective_time', 'desc')
            ->paginate(['list_rows' => $limit ?: 15, 'page' => $page ?: 1]);
        return_msg(200, '查询成功', [
            'list'  => $list->items(),
            'page'  => $list->currentPage(),
            'total' => $list->total(),
        ]);
    }

    /**
     * 人才列表
     * @throws \think\db\exception\DbException
     */
    public function talents()
    {
        $limit = input('limit');
        $page  = input('page');
        $list  = DataUserExtension::query()->order('id', 'desc')
            ->paginate(['list_rows' => $limit ?: 15, 'page' => $page ?: 1]);
        return_msg(200, '查询成功', [
            'list'  => $list->items(),
            'page'  => $list->currentPage(),
            'total' => $list->total(),
        ]);
    }

    /**
     * 查看简历或手机号
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getResumeOrPhone()
    {
        $userId = input('user_id');
        $type   = input('type');
        if (!$userId) {
            return_msg(404, '参数错误');
        }
        /**
         * @var DataUser $user
         */
        $user = DataUser::query()->with('extension')->where('id', $userId)->find();
        if (!$user) {
            return_msg(404, '用户不存在');
        }
        //查看用户关联的企业
        DataQiyeRzinfo::query()->where('user_id', $userId)->where('status', 1)
            ->where('deleted', 0)->find();
        if ($type === 'phone') {
            //添加用户被查看记录
            DataUserReview::query()->insert([
                'user_id'        => $userId,
                'review_user_id' => $this->uuid,
                'enterprise_id'  => $user,
            ]);
            return_msg(200, '', [
                'phone' => $user->phone,
            ]);
        }
        if ($type === 'resume') {
            $userExtension = $user->extension;
            if ($userExtension) {
                //添加用户被查看记录
                return_msg(200, '', [
                    'resume' => $userExtension->resume,
                ]);
            }
            return_error('该用户未上传简历');
        }
        return_msg(403, '参数错误');
    }

    /**
     * 加入我们
     */
    public function joinUs()
    {
        if (!$recruitmentId = input('recruitment_id')) {
            return_msg(404, '参数错误');
        }
        /**
         * @var RecruitmentTalent $recruitment
         */
        if (!$recruitment = RecruitmentTalent::query()->where('id', $recruitmentId)->find()) {
            return_msg(404, '招聘岗位不存在或已删除');
        }
        $review = RecruitmentReview::query()->where('user_id', $this->uuid)
            ->where('recruitment_id', $recruitmentId)->find();
        if (!$review) {
            RecruitmentReview::query()->insert([
                'user_id'        => $this->uuid,
                'recruitment_id' => $recruitmentId,
                'enterprise_id'  => $recruitment->getEnterpriseId(),
            ]);
        }

        return_msg(200, '提交成功');
    }

    /**
     * 发布人才招聘
     */
    public function recruitmentTalent()
    {
        $loginUser  = $this->user;
        $enterprise = DataQiyeRzinfo::query()->where('user_id', $loginUser['id'])->where('status', 1)
            ->where('deleted', 0)->find();
        if (!$enterprise) {
            return_msg(404, '当前用户非企业用户,请先认证企业再发布人才招聘');
        }
        $enterpriseId = input('enterprise_id');
        if (!$enterpriseId) {
            return_msg(404, '参数错误：请选择企业');
        }
        $recruitmentName = input('recruitment_name');
        if (RecruitmentTalent::query()->where('recruitment_name', $recruitmentName)->find()) {
            return_msg(404, '当前招聘岗位已存在');
        }
        $enterpriseTag = input('enterprise_tag');
        $jobTag        = input('job_tag');
        $data          = [
            'recruitment_name' => $recruitmentName,
            'enterprise_id'    => $enterpriseId,
            'poster'           => $loginUser['id'],
            'description'      => input('description'),
            'requirements'     => input('requirements'),
            'address'          => input('address'),
            'effective_time'   => input('effective_time') ?: date('Y-m-d H:i:s'),
            'is_expedited'     => input('is_expedited') ?: 0,
            'enterprise_tag'   => is_array($enterpriseTag) ? implode(',', $enterpriseTag) : '',
            'job_tag'          => is_array($jobTag) ? implode(',', $jobTag) : '',
        ];
        RecruitmentTalent::query()->insert($data);
        return_msg(200, '发布成功');
    }
}
